package com.shxt.controller;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.shxt.PageBean;
import com.shxt.model.User;
import com.shxt.service.UserService;

@Controller
@RequestMapping("/sys")
public class UserController {
	@Autowired
	private UserService userService;
	/**
	 * 将分页的员工数据传到user.jsp
	 * @param user
	 * @param map
	 * @param pages
	 * @return
	 */
	@RequestMapping(path="/userList", method={RequestMethod.GET,RequestMethod.POST})
	public String userList (User user,Map<String, Object> map,@RequestParam(required=false,defaultValue="1") int pages, String message) {
		map = PageBean.serverMap(map , user , pages);
        List<User> userList = userService.list(map);
        Integer count = userService.count(map);
        map = PageBean.map(map , user.getClass() , userList , count , pages);
        map.put("url" , "sys/userList");
        map.put("message",message);
        return "/user/user";
	}
	/**
	 * 查看单个员工的详细信息
	 * @param user_id
	 * @param model
	 * @return
	 */
	@RequestMapping("/getUser/{user_id}")
	public String getUser (@PathVariable Integer user_id, Model model) {
		model.addAttribute("getUser", userService.load(user_id));
		return "/user/getUser";
	}
	/**
	 * 删除员工
	 * @param user_id
	 * @return
	 */
	@RequestMapping("/delUser")
	@ResponseBody
	public int delUser (Integer user_id) {
		return userService.delete(user_id);
	}
	/**
	 * 跳转到添加员工界面
	 * @return
	 */
	@GetMapping("/addUser")
	public String addUser () {
		return "/user/addUser";
	}
	/**
	 * 添加员工
	 * @param user
	 * @param model
	 * @param response
	 * @return
	 */
	@PostMapping("/addUser")
	public String addUser (User user,Map<String, Object> map) {
		int n = userService.add(user);
		String m = "";
        if (n > 0) {
        	m = "添加成功";
        }else {
			m = "添加失败";
		}
        map.put("m", m);
		return "/user/addUser";
	}
	/**
	 * 跳转到修改员工界面
	 * @return
	 */
	@GetMapping("/updateUser")
	public String updateUser (Integer user_id,Model model) {
		model.addAttribute("getUser", userService.load(user_id));
		return "/user/updateUser";
	}
	/**
	 * 修改员工
	 * @param user
	 * @param model
	 * @param response
	 * @return
	 */
	@PostMapping("/updateUser")
	public String updateUser (User user,Map<String, Object> map) {
		int n = userService.update(user);
		String m = "";
        if (n > 0) {
        	m = "修改成功";
        }else {
			m = "修改失败";
		}
        map.put("m", m);
        map.put("getUser", userService.load(user.getUser_id()));
		return "/user/updateUser";
	}
	/**
     * 判断账号是否重复
     * @param account
     * @return
     */
	@RequestMapping("/account")
	@ResponseBody
	public int account (String account) {
		return userService.account(account);
	}
	/**
     * 判断身份证是否重复
     * @param account
     * @return
     */
	@RequestMapping("/identity")
	@ResponseBody
	public int identity (String identity) {
		return userService.identity(identity);
	}
	/**
	 * 上传照片
	 * @param user
	 * @param map
	 * @param user_photo
	 * @return
	 * @throws IllegalStateException
	 * @throws IOException
	 */
	@PostMapping("/photoUser")
    public String uploadUsers(User user,Map<String,Object> map,@RequestParam("myFile") MultipartFile user_photo) throws IllegalStateException, IOException{
    	String root = "C:/upload/crm";
    	String real_name = user_photo.getOriginalFilename();
    	String ext = FilenameUtils.getExtension(real_name);
    	String newName = UUID.randomUUID().toString().replaceAll("-", "")+"."+ext;
    	File file = new File(root+"/"+newName);
    	if(!file.exists()){
    		file.mkdirs();
    	}
    	user_photo.transferTo(file);
    	user.setUser_photo(newName);
        int result = userService.update(user);
        if(result>0){
            map.put("message" , "上传成功");
        }else {
            map.put("message" , "上传失败");
        }
        return "redirect:/sys/userList";
    }
}
